﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Way.Contract.MVC;
using Way.Bus.Model;
using Way.EAP.DataAccess.Data;
using System.Data;
using Way.Contract.Data.ControllerData;
using Way.Contract;
using Way.Bus.Controllers;

namespace Way.Bus.WMS.ST.Controllers
{
    public class ChartReportController : BusinessController
    {
        public ChartReportController() : base(new BasicInfo()) { }

        [WhereParameter]
        public string startdate { get; set; }
        [WhereParameter]
        public string enddate { get; set; }
        public string getCustProductNum() {
            List<PNoWithCustomer> list = new List<PNoWithCustomer>();
            BusinessBase bb = new BusinessBase();
            IDataAction ida=bb.GetDataAction();
            string strSql =
                "select t.ID,t.CUSTOMERNAME,SUM(t.INNUM) as INNUM,SUM(t.OUTNUM) as OUTNUM\n" +
                "from (\n" +
                "select t1.ID,t1.CUSTOMERNAME,sum(t3.NUM) as INNUM,0 as OUTNUM\n" +
                "from Acc_Bus_BusinessCustomer t1\n" +
                "left join Acc_WMS_InOrder t2\n" +
                "on t1.ID=t2.CLIENTNO\n" +
                "join Acc_WMS_InOrderMaterials t3\n" +
                "on t2.ID=t3.PARENTID\n" +
                "and t2.Creationdate between '" + startdate + "' and '" + enddate + "'\n" +
                "group by t1.ID,t1.CUSTOMERNAME\n" +
                "union\n" +
                "select t1.ID,t1.CUSTOMERNAME,0 as INNUM,sum(t3.NUM) as OUTNUM\n" +
                "from Acc_Bus_BusinessCustomer t1\n" +
                "left join Acc_WMS_OutOrder t2\n" +
                "on t1.ID=t2.CLIENTNO\n" +
                "join Acc_WMS_OutOrderMaterials t3\n" +
                "on t2.ID=t3.PARENTID\n" +
                "and t2.Creationdate between '" + startdate + "' and '" + enddate + "'\n" +
                "group by t1.ID,t1.CUSTOMERNAME\n" +
                ") t\n" +
                "group by t.ID,t.CUSTOMERNAME";

            DataTable dt = ida.GetDataTable(strSql);
            PNoWithCustomer pn;
            foreach(DataRow r in dt.Rows){
                pn = new PNoWithCustomer();
                pn.customerid = r["ID"].ToString();
                pn.customername = r["CUSTOMERNAME"].ToString();
                pn.innum = r["INNUM"].ToString();
                pn.outnum = r["OUTNUM"].ToString();
                list.Add(pn);
            }
            return JSON.Serializer(list.ToArray());
        }

        //员工业绩考核
        [WhereParameter]
        public string startdate1 { get; set; }
        [WhereParameter]
        public string enddate1 { get; set; }
        public string getWorkerKPI()
        {
            List<KPIByOfficeWorker> list = new List<KPIByOfficeWorker>();
            BusinessBase bb = new BusinessBase();
            IDataAction ida = bb.GetDataAction();
            string strSql =
                "select t1.ID,t1.WorkName,t3.RoleName,count(distinct t4.CLIENTNO) as CLIENTNUM,SUM(t5.NUM) as FINISHNUM,SUM(t6.NUM) as TOTALNUM\n" +
                "from Acc_Bus_OfficeWorker t1,\n" +
                "Acc_Bus_UserRole t2,\n" +
                "Acc_Bus_Role t3,\n" +
                "Acc_WMS_InOrder t4,\n" +
                "Acc_WMS_InOrderMaterials t5,\n" +
                "Acc_WMS_InNoticeMaterials t6\n" +
                "where t1.ID=t2.UserID\n" +
                "and t2.RoleID=t3.ID\n" +
                "and t4.Createdby=t1.ID\n" +
                "and t4.ID=t5.PARENTID\n" +
                "and t6.PARENTID = t5.SourceID\n" +
                "and t6.ID = t5.SourceRowID\n" +
                "and (t3.RoleName like '%库管员%' or t3.RoleName like '%管理员%')\n" +
                "and t4.Creationdate between '" + startdate1 + "' and '" + enddate1 + "'\n" +
                "group by t1.ID,t1.WorkName,t3.RoleName\n" +
                "order by t1.ID";

            DataTable dt = ida.GetDataTable(strSql);
            KPIByOfficeWorker kow;
            foreach (DataRow r in dt.Rows)
            {
                kow = new KPIByOfficeWorker();
                kow.workerid = r["ID"].ToString();
                kow.workername = r["WorkName"].ToString();
                kow.finishnum = r["FINISHNUM"].ToString();
                kow.totalnum = r["TOTALNUM"].ToString();
                list.Add(kow);
            }
            return JSON.Serializer(list.ToArray());
        }
    }

    

    public class PNoWithCustomer 
    {
        public string customerid{get; set;}
        public string customername {get; set;}
        public string innum { get; set; }
        public string outnum { get; set; }
    }

    public class KPIByOfficeWorker
    {
        public string workerid { get; set; }
        public string workername { get; set; }
        public string finishnum { get; set; }
        public string totalnum { get; set; }
    }
}
